package com.ihisun.tmhcms.core.service.housekeeperservice.impl;

import com.ihisun.tmhcms.core.dao.housekeeperdao.EditHousekeeperDao;
import com.ihisun.tmhcms.core.dao.mechanismdao.EditMechanismDao;
import com.ihisun.tmhcms.core.dto.housekeeperdto.EditHousekeeperDto;
import com.ihisun.tmhcms.core.dto.housekeeperdto.KeeperMechanismDto;
import com.ihisun.tmhcms.core.dto.housekeeperdto.SearchHousekeeperDto;
import com.ihisun.tmhcms.core.dto.mechanismdto.EditMechanismDto;
import com.ihisun.tmhcms.core.service.housekeeperservice.HousekeeperService;
import com.ihisun.tmhcms.core.vo.HousekeeperVo;
import com.ihisun.tmhcms.utils.DateUtils;
import com.ihisun.tmhcms.utils.PageBean;
import com.ihisun.tmhcms.utils.ResponseVo;
import com.ihisun.tmhcms.utils.UuIdUtil;
import org.springframework.security.core.parameters.P;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import javax.persistence.EntityManager;
import java.math.BigInteger;
import java.util.List;

@Service
public class HousekeeperServiceImpl implements HousekeeperService {
    @Resource
    EditHousekeeperDao editHousekeeperDao;
    @Resource
    EditMechanismDao editMechanismDao;
    @Resource
    EntityManager entityManager;

    @Override
    public ResponseVo editHousekeeper(EditHousekeeperDto editHousekeeperDto) {
        if (editHousekeeperDto.getKeeperId() != null && !"".equals(editHousekeeperDto.getKeeperId())) {
            editHousekeeperDto.setUpdateTime(DateUtils.getCurrentTime());
            editHousekeeperDao.save(editHousekeeperDto);
            List<KeeperMechanismDto> keeperMechanismDtos = editHousekeeperDto.getKeeperMechanismDtos();
            if (keeperMechanismDtos != null && !keeperMechanismDtos.isEmpty()) {
                for (int i = 0; i < keeperMechanismDtos.size(); i++) {
                    EditMechanismDto byMechanismId = editMechanismDao.findByMechanismId(keeperMechanismDtos.get(i).getMechanismId());
                    byMechanismId.setHousekeeperId(editHousekeeperDto.getKeeperId());
                    editMechanismDao.save(byMechanismId);
                }
            }

        } else {

            String uuid = UuIdUtil.getToUuid();
            editHousekeeperDto.setKeeperId(uuid);
            editHousekeeperDto.setUpdateTime(DateUtils.getCurrentTime());
            editHousekeeperDto.setCreateTime(DateUtils.getCurrentTime());
            editHousekeeperDao.save(editHousekeeperDto);
            List<KeeperMechanismDto> keeperMechanismDtos = editHousekeeperDto.getKeeperMechanismDtos();
            if (keeperMechanismDtos != null && !keeperMechanismDtos.isEmpty()) {
                for (int i = 0; i < keeperMechanismDtos.size(); i++) {
                    EditMechanismDto byMechanismId = editMechanismDao.findByMechanismId(keeperMechanismDtos.get(i).getMechanismId());
                    byMechanismId.setHousekeeperId(editHousekeeperDto.getKeeperId());
                    editMechanismDao.save(byMechanismId);
                }
            }

        }


        return ResponseVo.ok();
    }

    @Override
    public ResponseVo searchHousekeeper(SearchHousekeeperDto searchHousekeeperDto) {
        StringBuilder sql = new StringBuilder("select * from sys_housekeeper");
        if (searchHousekeeperDto.getName() != null && "".equals(searchHousekeeperDto.getName())) {
            sql.append(" and name like concat('%'," + searchHousekeeperDto.getName() + ",'%')");
        }
        if (searchHousekeeperDto.getPhone() != null && !"".equals(searchHousekeeperDto.getPhone())) {
            sql.append(" and phone like concat('%'," + searchHousekeeperDto.getPhone() + ",'%')");
        }
        if (searchHousekeeperDto.getHospital() != null && !"".equals(searchHousekeeperDto.getHospital())) {
            sql.append(" and keeper_id in (select housekeeper_id from m_mechanism where mechanism_name like concat('%'," + searchHousekeeperDto.getHospital() + ",'%') )");
        }
        sql.append("  limit " + (searchHousekeeperDto.getPageNum() - 1) * 10 + "," + searchHousekeeperDto.getPageSize() + "");
        List<HousekeeperVo> resultList = entityManager.createNativeQuery(sql.toString(), HousekeeperVo.class).getResultList();
        if (resultList != null && !resultList.isEmpty()) {
            for (int i = 0; i < resultList.size(); i++) {

                Integer integer = editMechanismDao.countByHousekeeperId(resultList.get(i).getKeeperId());
                if (integer == null) {
                    resultList.get(i).setMechanismId("0");
                } else {
                    resultList.get(i).setMechanismId(integer.toString());
                }

            }
        }
        StringBuilder countSql = new StringBuilder("select count(*) from sys_housekeeper");
        if (searchHousekeeperDto.getName() != null && "".equals(searchHousekeeperDto.getName())) {
            countSql.append(" and name like concat('%'," + searchHousekeeperDto.getName() + ",'%')");
        }
        if (searchHousekeeperDto.getPhone() != null && !"".equals(searchHousekeeperDto.getPhone())) {
            countSql.append(" and phone like concat('%'," + searchHousekeeperDto.getPhone() + ",'%')");
        }
        if (searchHousekeeperDto.getHospital() != null && !"".equals(searchHousekeeperDto.getHospital())) {
            countSql.append(" and keeper_id in (select housekeeper_id from m_mechanism where mechanism_name like concat('%'," + searchHousekeeperDto.getHospital() + ",'%') )");
        }

        BigInteger singleResult = (BigInteger) entityManager.createNativeQuery(countSql.toString()).getSingleResult();

        PageBean pageBean = new PageBean(searchHousekeeperDto.getPageNum(), singleResult.intValue(), searchHousekeeperDto.getPageSize(), resultList);

        return ResponseVo.ok(pageBean);
    }

    @Override
    public ResponseVo deleteHousekeerper(String keeperId) {
        Integer integer = editMechanismDao.countByHousekeeperId(keeperId);
        if (integer == null || integer.equals(0)) {
            editHousekeeperDao.deleteByKeeperId(keeperId);
            return ResponseVo.ok();
        }
        return ResponseVo.error();
    }
}
